﻿using Abp.Auditing;
using Abp.Domain.Repositories;
using Abp.Web.Models;
using Castle.Core.Logging;
using Magicodes.Admin.SFOrders;
using Magicodes.Admin.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml;

namespace Magicodes.Admin.Web.Controllers
{ 
    public class RouteController : AdminControllerBase
    {
        private readonly ILogger _logger;

        private readonly IRepository<SFQiaoRouteStatusBack, long> _sfQiaoStatusBackRepository;
        private readonly IRepository<SFQiaoRouteWaybillRoute, string> _sfQiaoRouteWaybillRouteRepository;

        public RouteController(  ILogger logger,
             IRepository<SFQiaoRouteStatusBack, long> sFQiaoStatusBackRepository,
             IRepository<SFQiaoRouteWaybillRoute, string> sFQiaoRouteWaybillRouteRepository 
            )
        {
            _logger = logger;
            _sfQiaoStatusBackRepository = sFQiaoStatusBackRepository;
            _sfQiaoRouteWaybillRouteRepository = sFQiaoRouteWaybillRouteRepository;

           // this.Response.ContentType = "text/plain; charset=utf-8";
        }


        /// <summary>
        /// 顺丰订单 状态推送
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [DontWrapResult]
        public ContentResult back2(string content)
        {
            this.Response.ContentType = "text/xml; charset=utf-8";
            try
            {
             
                _logger.Info("接受状态推送:");
                string xml = content;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                string json = JsonConvert.SerializeXmlNode(doc);
                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<SFQiaoStatusBackModel>(json);

                var entity=ObjectMapper.Map<SFQiaoRouteStatusBack>(data.Request);
                _sfQiaoStatusBackRepository.InsertAsync(entity);

                _logger.Info("接受状态推送完成");
                _logger.Info("xxxx:" + content);
                _logger.Info("xxxx:" + Newtonsoft.Json.JsonConvert.SerializeObject(data));
                 
                return Content(@"<?xml version='1.0' encoding='UTF-8' ?><Response><success>true</success></Response>");

            }
            catch (System.Exception ex)
            {
                
                _logger.Warn("接受状态推送", ex);
                return Content(@"<?xml version='1.0' encoding='UTF-8' ?><Response><success>false</success><msg>"+ex.Message+"</msg></Response>");
            }
           
        }

        /// <summary>
        /// 顺丰订单 状态推送
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [DontWrapResult]
        public async  Task<IActionResult> back(string content)
        {
            this.Response.ContentType = "text/xml; charset=utf-8";
            try
            {
                this.Response.ContentType = "text/xml; charset=utf-8";

               
               
                _logger.Info("接受路由推送:");
                string xml = content;
                    XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                string json = JsonConvert.SerializeXmlNode(doc);
                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<SFQiaoRouteModel>(json);


                var entity = ObjectMapper.Map<SFQiaoRouteWaybillRoute>(data?.Request?.Body?.WaybillRoute);
                
                 await _sfQiaoRouteWaybillRouteRepository.InsertAsync(entity);

                _logger.Info("接受路由推送完成");
                _logger.Info("xxxx:" + content);
                _logger.Info("xxxx:" + Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                return Content(@"<?xml version='1.0' encoding='UTF-8'?><Response service='RoutePushService' lang='zh-CN'><Head>OK</Head></Response>");
            }
            catch (System.Exception ex)
            {

                _logger.Warn("接受路由推送", ex);
                return Content(@"<?xml version='1.0' encoding='UTF-8'?><Response service='RoutePushService' lang='zh-CN'><Head>Error</Head></Response>" + ex.Message + "</msg></Response>");
            }
            return Content(@"<?xml version='1.0' encoding='UTF-8'?><Response service='RoutePushService' lang='zh-CN'><Head>Fail</Head></Response></msg></Response>");
        }
    }
}
